package org.dreamwork.seagragon.filesystem;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import java.lang.reflect.Method;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2010-4-24
 * Time: 18:14:12
 */
class HttpServletCallback implements MethodInterceptor {
    private ServletContext context;
    private ServletConfig config;

    static final String METHOD_GET_CONTEXT = "getServletContext";
    static final String METHOD_GET_CONFIG = "getServletConfig";

    HttpServletCallback (ServletContext context, ServletConfig config) {
        this.context = context;
        this.config = config;
    }

    public Object intercept (Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        if (context instanceof FSServletContext) {
            String name = method.getName ();
            if (METHOD_GET_CONTEXT.equals (name)) return context;
            if (METHOD_GET_CONFIG.equals (name)) return config;
            return methodProxy.invokeSuper (o, objects);
        } else {
            return methodProxy.invokeSuper (o, objects);
        }
    }
}
